package com.itheima.service.impl;
/*
    ヾ(❀ﾟ▽ﾟ)ノ 
 
 Grey1996year@163.com 
 
*/

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.mapper.SetmealMapper;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.JedisPool;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = SetmealService.class, protocol = "dubbo")
@Transactional
public class SetmealServiceImpl implements SetmealService {

	@Autowired
	private SetmealMapper setmealMapper;

	@Autowired
	private JedisPool jedisPool;


	@Override
	public void add(Setmeal setmeal, Integer[] checkgroupIds) {
		//先插入setmeal表
		setmealMapper.add(setmeal);

		//在插入中间表
		for (Integer checkgroupId : checkgroupIds) {
			System.out.println(setmeal.getId() + "--" + checkgroupId);
			HashMap<String, Integer> map = new HashMap<>();
			map.put("setmeal_id", setmeal.getId());
			map.put("checkgroup_id", checkgroupId);
			setmealMapper.addRelate(map);
		}

		// [2] 套餐成功添加后,将setmeal对象中的图片名称取出来，存在另一个redis集合中
		jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES, setmeal.getImg());
	}

	@Override
	public PageResult findPage(QueryPageBean queryPageBean) {
		Integer currentPage = queryPageBean.getCurrentPage();
		Integer pageSize = queryPageBean.getPageSize();
		String queryString = queryPageBean.getQueryString();
		//分页助手
		PageHelper.startPage(currentPage, pageSize);
		//
		Page<Setmeal> page = setmealMapper.findPageByString(queryString);

		return new PageResult(page.getTotal(), page.getResult());
	}

	@Override
	public List<Setmeal> findAll() {

		List<Setmeal> list = setmealMapper.findAll();

		return list;
	}

	@Override
	public Setmeal findById(Integer id) {
		Setmeal setmeal = setmealMapper.findById(id);

		return setmeal;
	}


	@Override
	public Map querySetmeal() {
		/*
		 "data":{
			"setmealNames":["套餐1","套餐2","套餐3"],
			"setmealCount":[
							{"name":"套餐1","value":10},
							{"name":"套餐2","value":30},
							{"name":"套餐3","value":25}
						   ]
				}
		 */
		//第一步 查出order表都有哪些setmeal_id
		List<Integer> setmealIds = setmealMapper.querySetmealId();
		//第二步 遍历集合 拿到每一个setmeal_id，作为接下来查询的参数
		List<Map<String, Object>> list_count = new ArrayList<>();
		List<String> list_name = new ArrayList<>();
		for (Integer setmealId : setmealIds) {
			//三 查询order表，该套餐被几个人预约
			int count = setmealMapper.querySetmealCount(setmealId);
			//四 查询setmeal表，查出对应id的套餐的名称
			String name = setmealMapper.querySetmealNames(setmealId);
			//封装setmealCount
			HashMap<String, Object> hashMap = new HashMap<>();
			hashMap.put("name", name);
			hashMap.put("value", count);
			list_count.add(hashMap);
			list_name.add(name);
		}
		//封装最终返回的结果
		HashMap<String, List> map = new HashMap<>();
		map.put("setmealNames", list_name);
		map.put("setmealCount", list_count);

		return map;
	}

	@Override
	public Result update(Setmeal setmeal, Integer[] checkgroupIds) {
		try {
			//先删除关联表的数据
			Integer setmealId = setmeal.getId();
			setmealMapper.deleteRelate(setmealId);
			//重新建立关联关系,传递多个参数，构造map
			for (Integer checkgroupId : checkgroupIds) {
				HashMap<String, Integer> map = new HashMap<>();
				map.put("setmeal_id", setmealId);
				map.put("checkgroup_id", checkgroupId);
				setmealMapper.addRelate(map);
			}
			//修改setmeal表
			setmealMapper.update(setmeal);
			return new Result(true, "修改套餐信息成功");
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(true, "修改套餐信息失败");
		}
	}

	@Override
	public void delete(Integer id) {
		//删除套餐
		//第一步 删除套餐与检查组的关联表
		setmealMapper.deleteRelate(id);
		//第二步 删除套餐与order表的关联表
		setmealMapper.deleteRelateWithOrder(id);
		//第三步 删除套餐
		setmealMapper.deleteSetmeal(id);
	}
}
